﻿--[[
   @brief    supr mult account mgr control
   @author   maxyao
   @version  1.0
   @date     2017-03-14
--]]

super_multi_account_mgr_control = {
    init = function ()
        ierd_tgp.tpf_ui.Require('super_multi_account_mgr\\model.lua');
        ierd_tgp.tpf_ui.Require('super_multi_account_mgr\\view.lua');
        ierd_tgp.tpf_ui.Require('wegame_banner\\model.lua');
    end;

    update_data = function()
        if super_multi_account_mgr_model.show_dlg == false then
            base_utility.log("[super_multi_account_mgr_control]show_dlg is null");
            return;
        end
        local multi_account_info = {
            primary_account_info = {},
            sub_account_info = {}
        };
        local game_id = super_multi_account_mgr_model.cur_game_id;
        local login_mgr =
            base_utility.get_component_adapter("ILogin_mgr",
                "ierd_tgp.login_mgr.GetLoginMgr();");
        local multi_account_list = ierd_tgp.login_mgr.SubAccountListPod:new();
        login_mgr:GetSubAccountList(multi_account_list);
        local account_info = ierd_tgp.login_mgr.AccountInfo:new_local();
        
        local primary_user = {};
        login_mgr:GetAccountInfo(super_multi_account_mgr_model.primary_uin, account_info);
        primary_user.uin = account_info.uin;
        primary_user.nick_name = account_info.nick_name;
        primary_user.head_img_url = account_info.avatar_url;
        primary_user.is_primary = true;
        primary_user.state = super_multi_account_mgr_control.get_primary_game_state();
        multi_account_info.primary_account_info = primary_user;
        -- 获取多开数量 TODO
        local gamelib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        for i = 0, (multi_account_list.count - 1), 1 do
            local sub_account_item = {};
            login_mgr:GetAccountInfo(multi_account_list.uins[i], account_info);
            sub_account_item.uin = account_info.uin;
            sub_account_item.nick_name = account_info.nick_name;
            sub_account_item.head_img_url = account_info.avatar_url;
            sub_account_item.is_primary = false;
            sub_account_item.state = gamelib:GetSubAccountLoginState(game_id, tonumber(account_info.uin));
            multi_account_info.sub_account_info[i] = sub_account_item;
        end
        local param_data = {};
        param_data.game_id = game_id;
        param_data.primary_uin = super_multi_account_mgr_model.primary_uin;
        param_data.sub_accounts_list = super_multi_account_mgr_control.get_sub_accounts_list();
        param_data.multi_account_info = multi_account_info;
        comm_center.send_broadcast("i_broadcast_update_super_multi_account_data", param_data);
        base_utility.log("[super_multi_account_mgr_control]update_data: "
             .. base_utility.table_to_json(param_data));
    end;

    add_sub_account = function ()
        local info = {
        game_id = super_multi_account_mgr_model.cur_game_id,
        from = "super_multi_account"};
        event_center.send_event(sub_account_login_event.SUB_ACCOUNT_ADD_ACCOUNT, base_utility.table_to_json(info));
        base_utility.log("[super_multi_account_mgr_control]add account.");
    end;

    update_sub_account_data = function (event_id, json_str)
        base_utility.log("[super_multi_account_mgr_control]add_sub_account_data:" .. json_str);
        local info = base_utility.json_to_table(json_str);
        local account_data = super_multi_account_mgr_control.get_account_data(info.uin)
        comm_center.send_broadcast("i_broadcast_super_multi_account_update_sub_account_data", account_data);
    end;

    get_account_data = function(uin)
        local account_data= {}
        local login_mgr =
            base_utility.get_component_adapter("ILogin_mgr",
                "ierd_tgp.login_mgr.GetLoginMgr();");
        local account_info = ierd_tgp.login_mgr.AccountInfo:new_local();
        login_mgr:GetAccountInfo(uin, account_info);
        account_data.uin = account_info.uin;
        account_data.nick_name = account_info.nick_name;
        account_data.head_img_url = account_info.avatar_url;
        if uin ~= super_multi_account_mgr_model.primary_uin then
            account_data.is_primary = false;
        else 
            account_datais.is_primary = true;
        end
        account_data.sub_accounts_list = super_multi_account_mgr_control.get_sub_accounts_list();
        return account_data;
    end;

    get_primary_game_state = function()
        local game_state = banner_model.get_game_state(super_multi_account_mgr_model.cur_game_id);
        local cur_state = 0;
        if game_state ~= nil then
            if game_state.iState == ierd_tgp.game_library.GAME_STATE_RUNNING then
                cur_state = 1;
            elseif game_state.iState == ierd_tgp.game_library.GAME_STATE_NORMAL then
                cur_state = 0;
            else 
                cur_state = 0;
            end;
        end;
        return cur_state;
    end;

    get_sub_accounts_list = function()
        local login_mgr =
            base_utility.get_component_adapter("ILogin_mgr",
                "ierd_tgp.login_mgr.GetLoginMgr();");
        if login_mgr == nil then 
            base_utility.log("[get_sub_accounts_list]login_mgr is nil get_sub_accounts_list failed");
            return;
        end
        local multi_account_list = ierd_tgp.login_mgr.SubAccountListPod:new();
        login_mgr:GetSubAccountList(multi_account_list);
        if multi_account_list == nil then 
            base_utility.log("[get_sub_accounts_list] GetSubAccountList failed");
            return;
        end;
        local sub_accounts_list_str = "";
        for i = 0, (multi_account_list.count - 1), 1 do
            if multi_account_list.uins[i] ~= nil then
                if sub_accounts_list_str == "" then
                    sub_accounts_list_str = tostring(multi_account_list.uins[i]);
                else
                    sub_accounts_list_str = sub_accounts_list_str .. ";" .. tostring(multi_account_list.uins[i]);
                end;
            end;
        end;
        return sub_accounts_list_str;
    end;

    update_primary_account_game_state = function(event_id, json_param)
        if json_param == nil then
            return;
        end
        local param = base_utility.json_to_table(json_param);
        if param == nil or param.game_id ~= super_multi_account_mgr_model.cur_game_id then
            return;
        end
        local primary_game_state = {};
        primary_game_state.state = super_multi_account_mgr_control.get_primary_game_state();
        primary_game_state.uin = super_multi_account_mgr_model.primary_uin;
        primary_game_state.game_id = super_multi_account_mgr_model.cur_game_id;
        comm_center.send_broadcast(
            "i_broadcast_super_multi_account_update_primary_account_game_state", primary_game_state);
    end;
    get_max_launch_account = function()
        local gamelib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        local count = gamelib:GetMultiLaunchCount(super_multi_account_mgr_model.cur_game_id);
        return count;
    end;

    del_sub_account = function(data)
        if data ~= nil and data.uin ~= nil then
            local login_mgr =
                base_utility.get_component_adapter("ILogin_mgr",
                "ierd_tgp.login_mgr.GetLoginMgr();");
            if login_mgr ~= nil then
                login_mgr:UpdateSubAccountLoginInfo(
                    super_multi_account_mgr_model.cur_game_id, data.uin,
                    super_multi_account_mgr_model.sub_account_opt.unbind);
            end;
            ierd_tgp.tpf_ui.Require('xiaohao\\sub_account_mgr.lua');
            sub_account_mgr.del_sub_account(data.uin);
            base_utility.log("[super_multi_account_mgr_control]delete account: " .. data.uin);
        end
    end;

    launch_game = function (data)
        if data == nil then 
            base_utility.log("[super_multi_account_mgr_control]launch_game param is nil");
            return
        end
        if data.game_id == nil or data.uin == nil then
            base_utility.log("[super_multi_account_mgr_control]launch_game param error: "
                .. base_utility.table_to_json(data));
            return
        end
        local game_id = data.game_id;
        local uin = data.uin;
        local primary_uin = super_multi_account_mgr_model.primary_uin;
        local op_from = super_multi_account_mgr_model.op_from;
        local gamelib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if uin == primary_uin then
            local server_id = gamelib:get_last_selected_server(game_id);
            gamelib:launch_game(game_id, tonumber(server_id), uin, op_from,"", ierd_tgp.game_library.ZONE_TYPE_FORMAL, "");
        else
            gamelib:try_switch_account(game_id, uin, op_from);
        end
    end;

    report_operation = function(op_type, op_uin)
        local qosparam = ierd_tgp.tpf_ui.LuaQosData:new();
        qosparam.oc_type = ierd_tgp.tpf_ui.kInstant;
        qosparam.qos_report_id = ierd_tgp.adapt_for_imports.qos.Qos_kind.kWebGameHelperMoreClient; -- 21749
        qosparam.int_val[0] = super_multi_account_mgr_model.report_sub_id;
        qosparam.int_val[1] = op_type;
        qosparam.int_val[2] = super_multi_account_mgr_model.cur_game_id;
        qosparam.int_val[3] = super_multi_account_mgr_model.primary_uin;
        if op_type == super_multi_account_mgr_model.op_type.OP_OPEN_SUPER_MULTI_ACCOUNT_DLG then
            qosparam.int_count = 4;
        else
            qosparam.int_val[4] = tonumber(op_uin);
            qosparam.int_count = 5;
        end
        qosparam.str_val1 = super_multi_account_mgr_control.get_sub_accounts_list();
        qosparam.str_val2 = tostring(super_multi_account_mgr_model.cur_game_id);
        qosparam.str_count = 2;
        base_utility.lua_qos(qosparam);
        qosparam:delete();
    end;

}

super_multi_account_mgr_control.init();